package com.xlongwei.info.util;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Comparator;
import java.util.stream.Collectors;

import com.networknt.service.SingletonServiceFactory;
import com.networknt.utility.StringUtils;
import com.xlongwei.info.model.Info;
import com.xlongwei.info.model.User;
import com.xlongwei.info.store.Store;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Users {

	public static Store store = SingletonServiceFactory.getBean(Store.class);

	/**
	 * 获取User信息
	 * 
	 * @param username
	 */
	public static User user(String username) {
		return store.user(username);
	}

	/**
	 * 登录（自动注册）
	 * 
	 * @param username
	 * @param password
	 * @return
	 */
	public static boolean login(String username, String password) {
		if (StringUtils.isBlank(username) || StringUtils.isBlank(password) || username.matches("1\\d{10}") == false) {
			return false;
		}
		User user = user(username);
		if (user == null) {
			user = User.builder().user(username).pass(password).owner(false).build();
			store.save(user);
			log.info("new user");
		} else {
			if (!password.equals(user.getPass())) {
				log.info("wrong pass {}", user.getPass());
				return false;
			} else if (user.isBan()) {
				log.info("ban user");
				return false;
			}
		}
		return true;
	}

	public static String ban(String username) {
		if (StringUtils.isBlank(username)) {
			return "user不能为空";
		}
		User user = store.user(username);
		if (user == null) {
			return username + "不存在";
		} else if (InfoConfig.INSTANCE.isAdmin(username)) {
			return "没有权限";
		}
		user.setBan(!user.isBan());
		store.save(user);
		return null;
	}

	public static String owner(User operator, String username) {
		if (StringUtils.isBlank(username)) {
			return "user不能为空";
		}
		User user = store.user(username);
		if (user == null) {
			return username + "不存在";
		} else if (InfoConfig.INSTANCE.isAdmin(user.getUser()) || !InfoConfig.INSTANCE.isAdmin(operator.getUser())) {
			return "没有权限";
		}
		user.setOwner(!user.isOwner());
		store.save(user);
		return null;
	}

	/**
	 * 接受或拒绝一条信息
	 * 
	 * @param operator
	 * @param user
	 * @return error错误提示 null成功
	 */
	public static String accept(User operator, String user) {
		if (StringUtils.isBlank(user)) {
			return "user不能为空";
		}
		Info info = Infos.info(user);
		if (info == null) {
			return "info不存在";
		} else if (operator.isBan()) {
			return "用户被禁";
		} else if (operator.isOwner() == false && info.isOwner() == false) {
			return "没有权限";
		}
		int startLock = InfoConfig.INSTANCE.getStartLockMinutes();
		int endGap = InfoConfig.INSTANCE.getEndGapMinutes();
		if (LocalDateTime.now().isAfter(info.getStart().plusMinutes(Math.min(startLock, endGap - 1)))) {
			return "行程已定";
		}
		boolean b = info.getAcceptors().contains(operator.getUser());
		if (b) {
			info.getAcceptors().remove(operator.getUser());
		} else {
			if ((info.isOwner() == false && info.getAcceptors().size() > 0)
					|| (info.isOwner() == true && info.getAcceptors().size() >= 4)) {
				return "人员已满";
			} else if (operator.getUser().equals(info.getUser())) {
				return "没有必要";
			}
			info.getAcceptors().add(operator.getUser());
		}
		return null;
	}

	public static Collection<User> users() {
		Collection<User> users = store.users();
		return users.stream().sorted(comparator).collect(Collectors.toList());
	}

	public static Comparator<User> comparator = new Comparator<User>() {
		// 排序：ADMIN > OWNER > USER
		@Override
		public int compare(User o1, User o2) {
			if (InfoConfig.INSTANCE.isAdmin(o1.getUser()) || (o1.isOwner() && !o2.isOwner())) {
				return -1;
			} else if (InfoConfig.INSTANCE.isAdmin(o2.getUser()) || (!o1.isOwner() && o2.isOwner())) {
				return 1;
			} else {
				return o1.getUser().compareTo(o2.getUser());
			}
		}

	};
}
